import { Platform, IpcAPI } from '@renderer/types/ipc';
import ElectronAdapter from './electronAdapter';
import WebAdapter from './webAdapter';

class PlatformDetector {
  static detect(): Platform {
    return 'web';
  }
}

class IPCService implements IpcAPI {
  private adapter: IpcAPI;
  private platform: Platform;

  constructor() {
    this.platform = PlatformDetector.detect();

    switch (this.platform) {
      default:
        this.adapter = new WebAdapter(); 
    }
    console.log(`IPC Service initialized for platform: ${this.platform}`);
  }

  get log() {
    return this.adapter.log;
  }

  downloadFile(payload: any): void {
    return this.adapter.downloadFile(payload);
  }

  saveBase64Image(params: { base64Data: string; directory: string; fileName: string; imagesPath: string }): Promise<any> {
    return this.adapter.saveBase64Image(params);
  }

  onDownloadProgress(callback: (progress: any) => void): () => void {
    return this.adapter.onDownloadProgress(callback);
  }

  onDownloadComplete(callback: (result: any) => void): () => void {
    return this.adapter.onDownloadComplete(callback);
  }

  onDownloadError(callback: (error: any) => void): () => void {
    return this.adapter.onDownloadError(callback);
  }

  readMetadata(filePath: string|File,map?:object): Promise<any> {
    return this.adapter.readMetadata(filePath,map);
  }

  readFolderMeta(): Promise<any> {
    return this.adapter.readFolderMeta();
  }

  getGlobalConfig(): Promise<any> {
    return this.adapter.getGlobalConfig();
  }

  saveConfig(config: any): Promise<any> {
    return this.adapter.saveConfig(config);
  }

  openFileDialog(): Promise<any> {
    return this.adapter.openFileDialog();
  }

  selectFileFolder(): Promise<any> {
    return this.adapter.selectFileFolder();
  }

  openFileFolder(folderPath: string): Promise<any> {
    return this.adapter.openFileFolder(folderPath);
  }

  scanDirectories(targetPath: string): Promise<any> {
    return this.adapter.scanDirectories(targetPath);
  }

  scanImages(basePath: string, folderName: string): Promise<any> {
    return this.adapter.scanImages(basePath, folderName);
  }

  scanLoras(loraPath: string): Promise<any> {
    return this.adapter.scanLoras(loraPath);
  }

  getLoraConfig(loraPath: string): Promise<any> {
    return this.adapter.getLoraConfig(loraPath);
  }

  saveLoraConfig(loraPath: string, data: any): Promise<any> {
    return this.adapter.saveLoraConfig(loraPath, data);
  }

  saveLoraImage(imagePath: string|File, loraPath: string): Promise<any> {
    return this.adapter.saveLoraImage(imagePath, loraPath);
  }

  saveWebpImage(imagePath: string|File, targetPath: string): Promise<any> {
    return this.adapter.saveWebpImage(imagePath, targetPath);
  }

  cacheImage(cachePath:string,array:ArrayBuffer|File):Promise<any> {
    return this.adapter.cacheImage(cachePath, array);
  }

  getLoraMetadata(loraPath: string): Promise<any> {
    return this.adapter.getLoraMetadata(loraPath);
  }

  getCivitaiMetadata(sha256:string|undefined,loraPath:string,replaceCover: boolean|undefined): Promise<any> {
    return this.adapter.getCivitaiMetadata(sha256,loraPath,replaceCover);
  }

  localSet(key: string, value: any): Promise<any> {
    return this.adapter.localSet(key, value);
  }

  localGet(key: string): Promise<any> {
    return this.adapter.localGet(key);
  }

  localRemove(key: string): Promise<any> {
    return this.adapter.localRemove(key);
  }

  localClear(): Promise<any> {
    return this.adapter.localClear();
  }

  sessionSet(key: string, value: any): Promise<any> {
    return this.adapter.sessionSet(key, value);
  }

  sessionGet(key: string): Promise<any> {
    return this.adapter.sessionGet(key);
  }

  sessionRemove(key: string): Promise<any> {
    return this.adapter.sessionRemove(key);
  }

  sessionClear(): Promise<any> {
    return this.adapter.sessionClear();
  }

  sendWebsocketData(data:any,type:string): Promise<any> {
    return this.adapter.sendWebsocketData(data,type);
  }

  onWebSocketConnection(callback: (event: any) => void): () => void {
    return this.adapter.onWebSocketConnection(callback);
  }

  onWebSocketDisconnection(callback: (event: any) => void): () => void {
    return this.adapter.onWebSocketDisconnection(callback);
  }

  onWebSocketMessage(callback: (event: any) => void): () => void {
    return this.adapter.onWebSocketMessage(callback);
  }

  broadcastMessage(message: string): Promise<any> {
    return this.adapter.broadcastMessage(message);
  }

  sendToClient(clientId: string, message: string,type:string): Promise<any> {
    return this.adapter.sendToClient(clientId, message,type);
  }

  getSocketClients(): Promise<any> {
    return this.adapter.getSocketClients();
  }

  onCsvImportProgress(callback: (progress: any) => void): void {
    return this.adapter.onCsvImportProgress(callback);
  }

  onImportProgress(callback: (progress: any) => void): ()=> void {
    return this.adapter.onImportProgress(callback);
  }
}

// 创建单例实例
export const ipcService = new IPCService();
